package com.prs.crm.action.oa;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.oa.OaTaskSource;
import com.prs.crm.service.oa.OaTaskSourceService;
import com.prs.crm.service.sys.LogService;

public class OaTaskSourceAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private OaTaskSource oaTaskSource;
	private OaTaskSourceService oaTaskSourceService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;


	@Action("listTaskSource")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(OaTaskSource.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOaTaskSourceService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editTaskSource")
	public String edit() {
		return SUCCESS;
	}
	
	@Action(value="saveTaskSource",results={@Result(name="success",type="redirect",location="listTaskSource"),
			@Result(name="error",type="chain",location="editTaskSource")})
	public String save() {
		if(oaTaskSource.getName()==null||"".equals(oaTaskSource.getName().trim())){
			addActionError("来源不能为空");
			return ERROR;
		}if (this.getLogService().getSearchName(OaTaskSource.class, oaTaskSource
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getOaTaskSourceService().save(oaTaskSource);
		return SUCCESS;
	}
	
	@Action(value="removeTaskSource",results={@Result(name="success",type="redirect",location="listTaskSource"),
			@Result(name="error",type="chain",location="listTaskSource")})
	public String remove() {
		if(ids == null) {
			this.addActionError("请选择要删除的工作来源");
			return ERROR;
		}
		for(Integer id : ids) {
			OaTaskSource source = this.getOaTaskSourceService().get(id);
			if(source.getOaTasks().size() != 0) {
				this.addActionError("已有任务引用，不能删除");
				return ERROR;
			}
		}
		this.getOaTaskSourceService().batchRemove(ids);
		return SUCCESS;
	}
	
	public OaTaskSource getOaTaskSource() {
		return oaTaskSource;
	}
	public void setOaTaskSource(OaTaskSource oaTaskSource) {
		this.oaTaskSource = oaTaskSource;
	}
	public OaTaskSourceService getOaTaskSourceService() {
		return oaTaskSourceService;
	}
	public void setOaTaskSourceService(OaTaskSourceService oaTaskSourceService) {
		this.oaTaskSourceService = oaTaskSourceService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}	
	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}

